﻿// CTitleWnd.cpp: 实现文件
//

#include "stdafx.h"
#include "DC-QC.h"
#include "CTitleWnd.h"
#include "ClientSocket.h"
#include "ServerSocket.h"
#include "WitsSocket.h"
#include "CTestDlg.h"
#include "CHisChromViewDlg.h"

#define TITLE_UP_HIGH             28
#define TITLE_TIME_SPACE          3
#define TITLE_TIME_BKCOLOR      RGB(51,51,51)
#define TITLE_TEXT_COLOR      RGB(255,255,255)
// CTitleWnd

IMPLEMENT_DYNCREATE(CTitleWnd, CFormView)

CTitleWnd::CTitleWnd()
	: CFormView(IDD_CTitleWnd)
{
	m_pServerSocket = NULL;
	m_pWitsInSocket = NULL;
}

CTitleWnd::~CTitleWnd()
{
	if (m_pWitsInSocket != NULL)
	{
		m_pWitsInSocket->Close();
		gl.ChromWits.nWitsInStatus = 0;
	}
	POSITION pos;
	for (pos = m_connectionList.GetHeadPosition(); pos != NULL;)
	{
		CClientSocket* pSock = (CClientSocket*)m_connectionList.GetNext(pos);
		pSock->Close() ;
	}
	if (m_pServerSocket != NULL)
		m_pServerSocket->Close();
}

void CTitleWnd::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CTitleWnd, CFormView)
	ON_WM_SIZE()
	ON_WM_TIMER()
	ON_WM_CREATE()
	ON_WM_LBUTTONDBLCLK()
END_MESSAGE_MAP()


// CTitleWnd 诊断

#ifdef _DEBUG
void CTitleWnd::AssertValid() const
{
	CFormView::AssertValid();
}

#ifndef _WIN32_WCE
void CTitleWnd::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif
#endif //_DEBUG


// CTitleWnd 消息处理程序


BOOL CTitleWnd::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
	// TODO: 在此添加专用代码和/或调用基类

	return CFormView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}



void CTitleWnd::OnDraw(CDC* pSDC)
{
	CRect rect;
	GetClientRect(rect);
	CBitmap*Oldmap;
	CBitmap cBitmap;
	CDC memDC;
	cBitmap.CreateCompatibleBitmap(pSDC, rect.Width(), rect.Height());
	memDC.CreateCompatibleDC(pSDC);
	Oldmap = memDC.SelectObject(&cBitmap);
	CDC* pDC = &memDC;
	OnPrepareDC(pDC);

	Draw(pDC);

	pSDC->SetViewportOrg(0, 0);
	pSDC->SetWindowOrg(0, 0);
	pSDC->SetMapMode(MM_TEXT);

	pDC->SetViewportOrg(0, 0);
	pDC->SetWindowOrg(0, 0);
	pDC->SetMapMode(MM_TEXT);

	CRect UpdateRect;
	pSDC->GetClipBox(UpdateRect);

	pSDC->BitBlt(UpdateRect.left, UpdateRect.top,
		rect.Width(), rect.Height(), pDC,
		UpdateRect.left, UpdateRect.top, SRCCOPY);
	memDC.SelectObject(Oldmap);
	memDC.DeleteDC();
}


void	CTitleWnd::Draw(CDC* pDC)
{
	CRect rect;
	GetClientRect(rect);
	BxGradientFill(pDC, rect, BACKCOLOR1, BACKCOLOR2,1);

	pDC->SetBkMode(TRANSPARENT);
	CFont* pF = pDC->SelectObject(&gl.m_pFont);
	pDC->SetTextColor(RGB(255,0,0));
	rect.top = 10*g_DPIScaleY;

	CRect rt = rect;
	rt.left = 50*g_DPIScaleX;
	pDC->DrawText(gl.m_strWellName, rt, DT_LEFT | DT_VCENTER | DT_SINGLELINE);
	
	pDC->SetTextColor(TITLE_TEXT_COLOR);
	rt.left = 0;
	pDC->DrawText(_T("中国石油渤海钻探工程有限公司"), rt, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

	//测试
	CTime tNow =  CTime::GetCurrentTime().GetTime();
	pDC->FillSolidRect(m_DateRect, TITLE_TIME_BKCOLOR);
	DrawInBorder(pDC, m_DateRect);
	pDC->SetTextColor(RGB(83, 255, 83));
	pDC->DrawText(tNow.Format(_T("%Y-%m-%d %H:%M:%S")), m_DateRect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
	pDC->SelectObject(pF);
	m_rtCompany = m_DateRect;

	CPoint pt1 = CPoint(5*g_DPIScaleX,12*g_DPIScaleY);
	CPoint dp;
	CSize s = m_dibCNPC.GetDimensions();

	dp.x = 0;
	dp.y = 0;
	m_dibCNPC.Draw(pDC, pt1, CSize(26*g_DPIScaleX,26*g_DPIScaleY), dp, s);
	m_rtCNPC = CRect(pt1, CSize(26*g_DPIScaleX, 26*g_DPIScaleY));
	//CString strTemp = GetSysString(7);
	//int nCanSta = 0;
	//if (strTemp == _T("OK"))
	//	nCanSta = 1;
	//strTemp = GetSysString(6);
	//int nDbSta = 0;
	//if (strTemp == _T("OK"))
	//	nDbSta = 1;

	//CFont* pOldFont = pDC->SelectObject(&m_pFont);
	//pDC->SetBkMode(TRANSPARENT);

	//pDC->SetTextColor(RGB(255, 7, 9));

	//pDC->TextOutW(rect.left + 20, rect.CenterPoint().y - 10, _T("IDS Version 2.1.0.0"));

	////CAN总线或数据库错误
	//if (nCanSta == 0 || nDbSta == 0)
	//{
	//	pDC->SetTextColor(RGB(255, 0, 0));
	//	if (nCanSta == 0)
	//	{
	//		pDC->TextOutW(rect.CenterPoint().x - 80, rect.CenterPoint().y - 10, _T("****CAN ERROR***"));
	//		//pDC->TextOutW(rect.CenterPoint().x,rect.CenterPoint().y-10,gl.m_strWellName );
	//	}
	//	else if (nDbSta == 0)
	//	{
	//		pDC->TextOutW(rect.CenterPoint().x - 80, rect.CenterPoint().y - 10, _T("****DB ERROR***"));
	//	}
	//}
	//else
	//{
	//	pDC->SetTextColor(RGB(0, 107, 109));
	//	pDC->TextOutW(rect.CenterPoint().x - 80, rect.CenterPoint().y - 10, _T("WELL ID:"));
	//	pDC->SetTextColor(RGB(0, 0, 0));
	//	pDC->TextOutW(rect.CenterPoint().x, rect.CenterPoint().y - 10, gl.m_strWellName);
	//}

	//if (m_bShowError)
	//{
	//	pDC->SetTextColor(RGB(255, 0, 0));
	//	pDC->FillSolidRect(CRect(rect.right - 180, rect.top + 1, rect.right, rect.bottom - 1), RGB(0, 255, 124));
	//}
	//else
	//	pDC->SetTextColor(RGB(44, 107, 109));
	//pDC->TextOutW(rect.right - 180, rect.CenterPoint().y - 10, m_strTime);
	//pDC->SelectObject(pOldFont);
}

void CTitleWnd::OnSize(UINT nType, int cx, int cy)
{
	CFormView::OnSize(nType, cx, cy);

	// TODO: 在此处添加消息处理程序代码
	CRect rect;
	GetClientRect(rect);
	m_DateRect.top = 12*g_DPIScaleY;
	m_DateRect.bottom = rect.bottom - 3*g_DPIScaleY;
	m_DateRect.right = rect.right - 4*g_DPIScaleX;
	m_DateRect.left = rect.right - 62*3*g_DPIScaleX;
}


void CTitleWnd::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (m_nTimer == nIDEvent)
	{
		if(m_strWN!=gl.m_strWellName)
		{
			m_strWN=gl.m_strWellName;
			Invalidate();
		}
		if (gl.gcStatus.nAnaStatus == 1 && (gl.curChromA.CAIndex > gl.gcMachine.dInjectTime*DATAFREQUENCE || gl.curChromB.CAIndex > gl.gcMachine.dInjectTime*DATAFREQUENCE))
		{
			//进入分析状态
			gl.gcStatus.nAnaStatus = 2;
		}
		//全烃计算
		if (gl.curChromA.CBIndex > 40 || gl.curChromB.CBIndex > 40)
			gl.CaculateTG();
		//CO2计算
		gl.CaculateCO2();

		if(gl.ChromWits.nWitsOutStatus)
			SendWits();

		InvalidateRect(m_DateRect);

		//流量计算
		for (int i = 0; i < 9; i++)
		{
			float dSig = 0;
			switch (i)
			{
			case 0:
				dSig = gl.gcStatus.m_dAAirP;
				break;
			case 1:
				dSig = gl.gcStatus.m_dAH21P;
				break;
			case 2:
				dSig = gl.gcStatus.m_dAH22P;
				break;
			case 3:
				dSig = gl.gcStatus.m_dBAirP;
				break;
			case 4:
				dSig = gl.gcStatus.m_dBH21P;
				break;
			case 5:
				dSig = gl.gcStatus.m_dBH22P;
				break;
			case 6:
				dSig = gl.gcStatus.m_dTGAirP;
				break;
			case 7:
				dSig = gl.gcStatus.m_dTGH2P;
				break;
			case 8:
				dSig = gl.gcStatus.m_dTGSampleP;
				break;
			}
			gl.gcMachine.sFlow[i].dValue = gl.CaculateFlow(i, dSig);
		}
	}
	CFormView::OnTimer(nIDEvent);
}


int CTitleWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFormView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码
	m_dibCNPC.LoadResource(IDB_CNPC);

	m_nTimer = SetTimer(9000, 1000, NULL);

#ifdef QC_CNNMACHINE
	//创建服务器
	int bRet;
	if (gl.ChromWits.nOutAutoOpen == 1)
		StartWitsOut();
	if (gl.ChromWits.nInAutoOpen == 1)
		OnConnectDLS();
#endif
	m_strWN=gl.m_strWellName;
	return 0;
}

//WITS通信
void CTitleWnd::ProcessPendingAccept()
{
	CClientSocket* pSocket = new CClientSocket(this);

	if (m_pServerSocket->Accept(*pSocket))
	{
		m_connectionList.AddTail(pSocket);
	}
	else
	{
		delete pSocket;
		AfxMessageBox(_T("Can not Accept Connection!"));
	}
}

void CTitleWnd::CloseWitsOutClient(CClientSocket* pSocket)
{
	POSITION pos, temp;
	for (pos = m_connectionList.GetHeadPosition(); pos != NULL;)
	{
		temp = pos;
		CClientSocket* pSock = (CClientSocket*)m_connectionList.GetNext(pos);
		if (pSock == pSocket)
		{
			m_connectionList.RemoveAt(temp);
			break;
		}
	}
}

//WITS结果发送
void CTitleWnd::SendWits()
{
	CString strBuffer = _T("&&\r\n");
	CString strTemp;
	for (int i = 0; i < 7; i++)
	{
		if (gl.m_nUnit == 0)
			strTemp.Format(_T("%s%.4f\r\n"), gl.ChromWits.strOutWits[i], gl.lastChromA.dResult[i]);
		else
			strTemp.Format(_T("%s%.0f\r\n"), gl.ChromWits.strOutWits[i], gl.lastChromA.dResult[i] * 10000);
		strBuffer = strBuffer + strTemp;
	}
	if (gl.m_nWorkMode == 3)
	{
		for (int i = 0; i < 7; i++)
		{
			if (gl.m_nUnit == 0)
				strTemp.Format(_T("%s%.4f\r\n"), gl.ChromWits.strOutWits[9+i], gl.lastChromB.dResult[i]);
			else
				strTemp.Format(_T("%s%.0f\r\n"), gl.ChromWits.strOutWits[9 + i], gl.lastChromB.dResult[i] * 10000);
			strBuffer = strBuffer + strTemp;
		}
	}
	//全烃
	if (gl.m_nUnit == 0)
		strTemp.Format(_T("%s%.4f\r\n"), gl.ChromWits.strOutWits[7], gl.m_dTGResult);
	else
		strTemp.Format(_T("%s%.0f\r\n"), gl.ChromWits.strOutWits[7], gl.m_dTGResult * 10000);
	strBuffer = strBuffer + strTemp;
	//CO2
	if (gl.m_nUnit == 0)
		strTemp.Format(_T("%s%.4f\r\n"), gl.ChromWits.strOutWits[8], gl.m_dCO2Result);
	else
		strTemp.Format(_T("%s%.0f\r\n"), gl.ChromWits.strOutWits[8], gl.m_dCO2Result * 10000);
	strBuffer = strBuffer + strTemp;
	strBuffer = strBuffer + _T("!!\r\n");
	gl.ChromWits.strWitsOutBuf = strBuffer;
	int buffersize = WideCharToMultiByte(CP_ACP, NULL, strBuffer, -1, NULL, 0, NULL, FALSE);
	POSITION pos;
	for (pos = m_connectionList.GetHeadPosition(); pos != NULL;)
	{
		CClientSocket* pSock = (CClientSocket*)m_connectionList.GetNext(pos);
		int dwBytes;
		if ((dwBytes = pSock->Send(wchar2char(strBuffer), buffersize)) == SOCKET_ERROR)
		{
			m_connectionList.RemoveAt(pos);
			break;
		}
	}
}

void CTitleWnd::StartWitsOut()
{
	if (gl.ChromWits.nOutMode == 0)
	{
		m_pServerSocket = new CServerSocket(this);
		if (!m_pServerSocket->Create(gl.ChromWits.nOutPort))		//8667
			AfxMessageBox(_T("不能创建WITS OUT 输出端口!"));
		if (!m_pServerSocket->Listen(5))
		{
			AfxMessageBox(_T("不能创建WITS OUT 网络监听线程!"));
		}
		gl.ChromWits.nWitsOutStatus = 1;
	}
}

void CTitleWnd::OnConnectDLS()
{
	if (gl.ChromWits.nInMode==1)
	{
		if (m_pWitsInSocket == NULL)
		{
			m_pWitsInSocket = new CWitsSocket(this);
			m_pWitsInSocket->Create();
		}
		if (m_pWitsInSocket->Connect(gl.ChromWits.strInIP, gl.ChromWits.nInPort))
			gl.ChromWits.nWitsInStatus = 1;
		else
		{
			AfxMessageBox(_T("不能以WITS 客户端方式连接综合录井仪软件系统！"), MB_ICONINFORMATION);
			gl.ChromWits.nWitsInStatus = 0;
		}
	}
	else
	{
		if (m_pWitsInSocket != NULL)
		{
			m_pWitsInSocket->Close();
			gl.ChromWits.nWitsInStatus = 0;
		}
	}
}

void CTitleWnd::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (m_rtCNPC.PtInRect(point))
	{
		if (AfxMessageBox(_T("请确认退出 双通道快速色谱工作站软件？"), MB_YESNO | MB_ICONSTOP) == IDYES)
		{
			//关闭EPC
			cControl.CloseEPC();
			Sleep(1000);

			CString strFileName;
			strFileName.Format(_T("%s\\SystemMethod.mtd"), m_strAppPath);
			gl.SaveCalMethod(strFileName);

			gl.SaveSoftwareSetting();

			if (m_pCnnWell->GetState() == 1)
			{
				m_pCnnWell->Close();
				m_pCnnWell = NULL;
			}
			WriteLog(_T("双通道色谱软件关闭******"));
			exit(0);
		}
	}

	if (m_rtCompany.PtInRect(point))
	{
		//CHisChromViewDlg dlg;
		CTestDlg dlg;
		dlg.DoModal();
	}

	CFormView::OnLButtonDblClk(nFlags, point);
}
